#include <iostream>

using namespace std;

typedef long long LL;

const LL MOD = 1e9 + 7;

int T;
LL n, k;

LL qpow(LL a, LL b, LL c)
{
	LL ret = 1;
	while(b)
	{
		if(b & 1) ret = ret * a % c;
		a = a * a % c;
		b >>= 1;
	}
	return ret;
}

int main()
{
	cin >> T;
	while(T--)
	{
		cin >> n >> k;
		LL ret = (qpow(k + 1, n + 1, MOD) - k - 1) * qpow(k, MOD - 2, MOD);
		cout << (ret % MOD + MOD ) % MOD << endl;
	}
	return 0;
}
